/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Grade;
import db.dao.StudentDAO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * Lists grades of courses for the currently logged in student.
 * @author Mikael
 */
public class GradeDataTableBean {
    private ArrayList<Grade> grades;
    private int headerCnt;
    //private String currHeader;

    public static int COLUMN_COUNT = 3;
    private String[] headers = new String[COLUMN_COUNT];

    public String getCurrHeader(){
        String header = headers[headerCnt];
        headerCnt++;
        headerCnt %= COLUMN_COUNT;
        return header;
    }

    @PostConstruct
    public void init(){
        headers[0] = "Code";
        headers[1] = "Course Name";
        headers[2] = "Grade";

        grades = new ArrayList();
    }

    public List<Grade> getGrades(){
        //Get active login id by getting login session bean
        FacesContext facesCont = FacesContext.getCurrentInstance();
        HttpSession https = (HttpSession)facesCont.getExternalContext().getSession(false);
        LoginBean lb = (LoginBean)https.getAttribute(LoginBean.MANGED_BEAN_NAME);
        
        grades.clear();
        grades = (ArrayList)StudentDAO.getGrades(lb.getId());

        return grades;
    }
    public void setGrades(ArrayList<Grade> g){
        this.grades = g;
    }
}
